package com.example.qxfw.yjqx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.zy.CshxSetting;
import com.example.qxfw.common.util.FileUtil;
import com.example.qxfw.cshx.service.CshxSettingService;
import com.example.qxfw.yjqx.service.CshxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.List;

@ApiIgnore
@Api(tags = "应急气象-城市火险")
@RestController
@CrossOrigin
@RequestMapping("/cshx")
public class CshxController {


    @Autowired
    private CshxService cshxService;

    @ApiOperation("火险预报")
    @GetMapping("/ybzz")
    public SysResult ybzz(){

        return SysResult.success();
    }

    @ApiOperation("生成产品")
    @PostMapping ("/createProduct")
    public Object createProduct(String date,String userName, MultipartFile[] images){
        try {
            return cshxService.createProduct(date, userName, images);
        } catch (Exception e){
            e.printStackTrace();
            System.err.println("产品生成失败");
            return null;
        }
    }

    @ApiOperation("模板下载")
    @GetMapping("/getTemplate")
    public Object getTemplate(){
        ApplicationHome applicationHome = new ApplicationHome(FileUtil.class);
        String templatePath = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() +
                "\\src\\main\\resources\\Documents\\城市火险等级预报模板\\城市火险等级预报模板.docx";
        // 设置前端响应
        byte[] buffer = new byte[1024];
        int bytesRead;
        try (
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                FileInputStream inputStream = new FileInputStream(templatePath);
        ) {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            String fileName = URLEncoder.encode("森林火险预警文档模板", "UTF-8").replaceAll("\\+", "%20");
            headers.setContentDispositionFormData("attachment", fileName+".docx");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentLength(byteArray.length);
            return new ResponseEntity<>(byteArray, headers, HttpStatus.OK);
        } catch (Exception e) {
            System.err.println("模板下载失败");
        }
        return null;
    }



}
